package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.TbSpecGroup;
import com.leyou.item.entity.TbSpecParam;
import com.leyou.item.mapper.TbSpecParamMapper;
import com.leyou.item.service.TbSpecGroupService;
import com.leyou.item.service.TbSpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 规格参数组下的参数名 服务实现类
 * </p>
 *
 * @author SYL
 * @since 2020-02-11
 */
@Service
public class TbSpecParamServiceImpl extends ServiceImpl<TbSpecParamMapper, TbSpecParam> implements TbSpecParamService {

    @Override
    public List<SpecParamDTO> findSpecParamByCategoryIdOrGroupId(Long gid, Long cid, Boolean searching) {
//        gid和cid必须保持有一个值
//        判断gid和cid是否同时为空，如果是抛异常
        QueryWrapper<TbSpecParam> queryWrapper = new QueryWrapper<>();
        if(gid==null&&cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        if(gid!=null){
//            构建gid的查询条件
            queryWrapper.lambda().eq(TbSpecParam::getGroupId,gid);
        }
        if(cid!=null){
//            构建cid的查询条件
            queryWrapper.lambda().eq(TbSpecParam::getCid,cid);
        }
        if(searching!=null){
//            构建searching的查询条件
            queryWrapper.lambda().eq(TbSpecParam::getSearching,searching);  //true--->1  false --->0
        }

        List<TbSpecParam> tbSpecParamList = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(tbSpecParamList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(tbSpecParamList,SpecParamDTO.class);
    }

    
    @Autowired
    private TbSpecGroupService specGroupService;
    @Override
    public List<SpecGroupDTO> findSpecGroupWithParamsByCategoryId(Long cid) {

        QueryWrapper<TbSpecGroup> queryWrapper = new QueryWrapper<>();
//            构建cid的查询条件
        queryWrapper.lambda().eq(TbSpecGroup::getCid,cid);
        List<TbSpecGroup> tbSpecGroupList = specGroupService.list(queryWrapper);
        if(CollectionUtils.isEmpty(tbSpecGroupList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(tbSpecGroupList, SpecGroupDTO.class);
     // 以上的集合中每个SpecGroupDTO的对象中 params属性值还有数据
//        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {  //这种代码有弊端  如果规格组集合长度是10，意味着需要查询10次规格参数
//            List<SpecParamDTO> params = this.findSpecParamByCategoryIdOrGroupId(specGroupDTO.getId(), null, null);
//            specGroupDTO.setParams(params);
//        }
        List<SpecParamDTO> paramList = this.findSpecParamByCategoryIdOrGroupId(null, cid, null);
//        paramList//有所有规格组下的规格参数  分组 把这么写规格参数根据groupId分组
        Map<Long, List<SpecParamDTO>> paramMapByGroup = paramList.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

//        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
//            List<SpecParamDTO> specParamDTOS = paramMapByGroup.get(specGroupDTO.getId());
//            specGroupDTO.setParams(specParamDTOS);
//        }
        specGroupDTOS = specGroupDTOS.stream().map(group -> {
            List<SpecParamDTO> paramDTOS = paramMapByGroup.get(group.getId());
            group.setParams(paramDTOS);
            return group;
        }).collect(Collectors.toList());

        return specGroupDTOS;
    }
}
